package com.sam.spring.chapter1.helloworld;

import org.junit.Assert;
import org.junit.Test;
import org.springframework.beans.factory.BeanFactory;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class HelloTest {
	@Test
	public void testHelloWorld() {
		// 1、读取配置文件实例化一个IoC容器
		ApplicationContext context = new ClassPathXmlApplicationContext("chapter1/helloworld.xml");
		// 2、从容器中获取Bean，注意此处完全“面向接口编程，而不是面向实现”
		HelloAPI helloApi = context.getBean("hello", HelloAPI.class);
		// 3、执行业务逻辑
		helloApi.sayHello();
	}

	@Test
	public void test01() {
		BeanFactory beanFactory = new ClassPathXmlApplicationContext("chapter1/helloworld.xml");
		// 根据类型获取bean
		HelloAPI helloAPI = beanFactory.getBean(HelloAPI.class);
		helloAPI.sayHello();
	}
	
	@Test
	public void test02() {
		BeanFactory beanFactory = new ClassPathXmlApplicationContext(
				"chapter1/helloworld.xml");
		// 根据id获取bean
		HelloAPI bean1 = beanFactory.getBean("hello", HelloAPI.class);
		bean1.sayHello();
		// 根据别名获取bean
		HelloAPI bean2 = beanFactory.getBean("alias1", HelloAPI.class);
		bean2.sayHello();
		// 根据id获取bean
		HelloAPI bean3 = beanFactory.getBean("hello", HelloAPI.class);
		bean3.sayHello();
		String[] bean3Alias = beanFactory.getAliases("hello");
		// 因此别名不能和id一样，如果一样则由IoC容器负责消除冲突
		Assert.assertEquals(0, bean3Alias.length);
	}
	
	@Test
	public void test03() {
		BeanFactory beanFactory = new ClassPathXmlApplicationContext(
				"chapter1/helloworld.xml");
		// 1根据id获取bean
		HelloAPI bean1 = beanFactory.getBean("bean1", HelloAPI.class);
		bean1.sayHello();
		// 2根据别名获取bean
		HelloAPI alias11 = beanFactory.getBean("alias11", HelloAPI.class);
		alias11.sayHello();
		// 3验证确实是四个别名
		String[] bean1Alias = beanFactory.getAliases("bean1");
		System.out.println("=======helloworld.xml bean1 别名========");
		for (String alias : bean1Alias) {
			System.out.println(alias);
		}
		Assert.assertEquals(4, bean1Alias.length);
		
		// 根据id获取bean
		HelloAPI bean2 = beanFactory.getBean("bean2", HelloAPI.class);
		bean2.sayHello();
		// 2根据别名获取bean
		HelloAPI alias21 = beanFactory.getBean("alias21", HelloAPI.class);
		alias21.sayHello();
		// 验证确实是两个别名
		String[] bean2Alias = beanFactory.getAliases("bean2");
		System.out.println("=======helloworld.xml bean2 别名========");
		for (String alias : bean2Alias) {
			System.out.println(alias);
		}
		Assert.assertEquals(2, bean2Alias.length);
	}
	
	@Test
	public void test04() {
		BeanFactory beanFactory = new ClassPathXmlApplicationContext(
				"chapter1/helloworld.xml");
		// 根据id获取bean
		HelloAPI bean = beanFactory.getBean("bean", HelloAPI.class);
		bean.sayHello();
		// 根据别名获取bean
		HelloAPI alias1 = beanFactory.getBean("alias1", HelloAPI.class);
		alias1.sayHello();
		HelloAPI alias2 = beanFactory.getBean("alias2", HelloAPI.class);
		alias2.sayHello();
		
		String[] beanAlias = beanFactory.getAliases("bean");
		System.out.println("=======helloworld.xml bean 别名========");
		for (String alias : beanAlias) {
			System.out.println(alias);
		}
		System.out.println("=======helloworld.xml bean 别名========");
		Assert.assertEquals(2, beanAlias.length);
	}
	
	@Test
	public void test05() {
		// 使用构造器
		BeanFactory beanFactory = new ClassPathXmlApplicationContext(
				"chapter1/helloworld.xml");
		HelloAPI bean1 = beanFactory.getBean("bean3", HelloAPI.class);
		bean1.sayHello();
		HelloAPI bean2 = beanFactory.getBean("bean4", HelloAPI.class);
		bean2.sayHello();
	}
	
	@Test
	public void testInstantiatingBeanByStaticFactory() {
		// 使用静态工厂方法
		BeanFactory beanFactory = new ClassPathXmlApplicationContext(
				"chapter1/helloworld.xml");
		HelloAPI bean3 = beanFactory.getBean("bean5", HelloAPI.class);
		bean3.sayHello();
	}
	
	@Test
	public void testInstantiatingBeanByInstanceFactory() {
		// 使用实例工厂方法
		BeanFactory beanFactory = new ClassPathXmlApplicationContext(
				"chapter1/helloworld.xml");
		HelloAPI bean4 = beanFactory.getBean("bean6", HelloAPI.class);
		bean4.sayHello();
	}
}
